
var VoaGeo = {
  initialize: function(map_div_id, news_div_id, debug_div_id) {
		this.map=new GMap2(document.getElementById(map_div_id),{draggableCursor: 'crosshair', draggingCursor: 'move'});
		this.newsDiv = document.getElementById(news_div_id);
		if (debug_div_id != null) {
			this.debugDiv = document.getElementById(debug_div_id);
		} else {
			this.debugDiv = null;
		}
		var point = new GLatLng(34, 0);
		this.map.setCenter(point, 2);
		this.map.addControl(new GSmallMapControl());
		this.map.addControl(new GMapTypeControl());
		this.map.enableScrollWheelZoom();
		this.map.enableDoubleClickZoom();
		this.map.enableContinuousZoom();
  },

	debug: function(msg) {
	  if (this.debugDiv != null) {
	    this.debugDiv.innerHTML += msg + "<br>";
	  }
	},
	
	addItem: function(voaItem, itemIdx) {
		this.newsDiv.innerHTML += "<a target='_blank' href='" + voaItem.link + "' onclick='return VoaChanel.selectItem(" + itemIdx + ")'>" + voaItem.title + "</a><br>";
		if (voaItem.marker) {
			this.map.addOverlay(voaItem.marker);
		}
	},
	
	reset:function() {
		this.newsDiv.innerHTML = "<img src='http://golyovoa.googlecode.com/svn/trunk/picture/voa_logo_50.jpg'/><br>";
		this.map.clearOverlays();
	}
};

function VOAItem(xmlItem) {
	this.title = GXml.value(xmlItem.getElementsByTagName("title")[0]);
	this.link = GXml.value(xmlItem.getElementsByTagName("link")[0]);
	this.coverage = GXml.value(xmlItem.getElementsByTagName("dc:coverage")[0]);
	this.description = GXml.value(xmlItem.getElementsByTagName("description")[0]);
}

VOAItem.prototype.toString = function() {
	return this.coverage + ":" + this.title;
}

VOAItem.prototype.createMarkerByPos = function(response) {
	if (response) {
		var t = this.title;
		this.marker = new GMarker(response, {title: t});
		this.marker.bindInfoWindow("<div><a target='_blank' href='" + this.link + "'>" + this.title + "</a><br>" + this.description + "</div>");		
		VoaGeo.map.addOverlay(this.marker);
	} else {
		this.marker = null;
	}
}

function bindMethod(method, scope){
	return function(){
		method.apply(scope,arguments);
	}
}

var VoaChanel = {
	initialize: function(planguage, pchanel) {
		this.geocoder = new GClientGeocoder();
		this.language = planguage;
		this.chanel = pchanel;
		this.voaChanels = {};
		this.loadNews();
	},
	selectLanguage: function(planguage) {
		this.language = planguage;
		this.loadNews();
	},
	
	selectChanel: function(pchanel) {	
		this.chanel = pchanel;
		this.loadNews();
	},
	
	loadNews:function() {
		VoaGeo.reset();
		var voaId = this.language + "_" + this.chanel;
		this.voaItems = this.voaChanels[voaId];
		if (this.voaItems != null) {
			VoaGeo.debug("found");
		  for (var i = 0; i < this.voaItems.length; i++) {
		  	VoaGeo.addItem(this.voaItems[i], i);
		  }				
		} else {
			var url = "data/topstories.xml";
			VoaGeo.debug("load:" + url);
			GDownloadUrl(url, bindMethod(this.parseXml, VoaChanel));
		}
	},
	
	parseXml: function(data, responsCode) {
		this.voaItems = new Array();		
		var xml = GXml.parse(data);
		var items = xml.documentElement.getElementsByTagName("item");
	  for (var i = 0; i < items.length; i++) {
			var it = new VOAItem(items[i]);
			this.voaItems[i] = it;
		  VoaGeo.addItem(it, i);
			this.geocoder.getLatLng(it.coverage, bindMethod(it.createMarkerByPos, it));
	  }
	  var voaId = this.language + "_" + this.chanel;
	  this.voaChanels[voaId] = this.voaItems;
	},
	
	selectItem: function(itemIdx) {
		var item = this.voaItems[itemIdx];
		if (item.marker) {
			GEvent.trigger(item.marker, "click");
			return false;
		}
		return true;
	}
}

